package chapter7;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 利用正则表达式做字符串替换
 */
public class ReplaceTest {
    public static void main(String[] args) {
        String[] msgs = new String[] {
                "Java has regular expressions in 1.4",
                "regular expressions now expressing in Java",
                "Java represses oracular expressions"
        };
        Pattern p = Pattern.compile("re\\w*");
        Matcher matcher = null;
        for(String msg : msgs) {
            if(matcher == null) {
                matcher = p.matcher(msg);
            } else {
                matcher.reset(msg);
            }
            System.out.println(matcher.replaceAll("哈哈:"));
        }
    }
}
